import Taro, { Component } from "@tarojs/taro";
import { View } from "@tarojs/components";
import { connect } from "@tarojs/redux";
import { AtNavBar,AtMessage } from "taro-ui";

import { onCheckPlan } from "../../actions/checkPlan";
import PlanAccordion from "../../components/PlanAccordion/planAccordion";
import "./finishedPlan.less";
import OverridePlanModal from '../../components/OverridePlanModal/overridePlanModal'
//state不支持计算属性
@connect(
  state => ({
    checkPlan:state.checkPlan,
    makePlan:state.makePlan
  }),
  { onCheckPlan }
)
class FinishedPlan extends Component {
  constructor(props) {
    super(props);
    this.state = {
      setStateOnce: () => {}
    };
  }
  componentWillMount() {
    let params = this.$router.params;
    let data = {
      user: params.user,
      days: params.days === "" ? [] : [params.days]
    };
    this.props.onCheckPlan(data);
  }

  setStateOnce = () => {
    let ran;
    return function(planList) {
      if (!ran) {
        planList.forEach(e => {
          this.setState({
            [e.executeDay]: false
          });
        });
        ran = true;
      }
    }.bind(this);
  };

  componentDidMount() {
    this.setState({
      setStateOnce: this.setStateOnce()
    });
  }

  render() {
    const planList = this.props.checkPlan.checkPlans;
    planList.length === 0 ? null : this.state.setStateOnce(planList);
    return (
      <View className='FinishedPlan'>
        {this.props.makePlan.unsubmitedPlans &&
        this.props.makePlan.unsubmitedPlans[1].length !== 0 ? (
          <OverridePlanModal />
        ) : null}
        <AtNavBar
          title='已制定的计划'
          fixed
          leftIconType='chevron-left'
          onClickLeftIcon={() => {
            Taro.redirectTo({
              url: '/pages/index/index'
            });
          }}
        />
        <AtMessage />
        <View>
          {
            planList.length === 0 ? (
            <View>不存在的</View>
          ) : planList.map((v, i) => <PlanAccordion key={i} v={v} />)
          }
        </View>
      </View>
    );
  }
}

export default FinishedPlan;
